package cn.lingyangwl.agile.msg.push.core.sender;

import cn.lingyangwl.agile.msg.push.api.constatnts.ChannelTypeEnum;
import cn.lingyangwl.agile.msg.push.api.constatnts.MsgPushStatus;
import cn.lingyangwl.agile.msg.push.core.PushTaskContext;
import cn.lingyangwl.agile.msg.push.core.channel.ChannelClientManager;
import cn.lingyangwl.agile.msg.push.core.channel.client.AbstractSmsMsgClient;
import cn.lingyangwl.agile.msg.push.model.rqrs.channel.SmsChannelResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author shenguangyang
 */
@Slf4j
@Service
public class SmsSender implements MsgSender {
    @Resource
    private ChannelClientManager channelClientManager;

    @Override
    public void sendMessage(PushTaskContext context) {
        AbstractSmsMsgClient channelClient = (AbstractSmsMsgClient) channelClientManager.getChannelClient(context.getChannelType());
        context.setChannelCode(channelClient.channelCode());

        SmsChannelResp ret = channelClient.send(context);

        context.setPushResult(ret.getContent());
        if (!ret.isSuccess()) {
            context.setPushStatus(MsgPushStatus.FAIL);
        }
    }

    @Override
    public ChannelTypeEnum channelType() {
        return ChannelTypeEnum.SMS;
    }
}
